<template>
  <section class="todoapp">
    <!-- 除了驼峰, 还可以使用-转换链接 -->
    <TodoHeader @add-todo="addFn" />
    <!-- 筛选出来的list 最后返回到显示的组件上实时更新 -->
    <!-- 子组件删除数据 派发到父组件这里进行数据处理 -->
    <TodoMain :list="showList" @del-todo="delTotoFn" />
    <TodoFooter :list="list" @show-todo="changeType" @clearDoList="clearDoFn" />
    <!-- 传递过来type状态 在这边接收一下 根据type状态筛选数据 -->
    <div></div>
  </section>
</template>

<script>
// 1.0 样式引入
import "./assets/css/base.css";
import "./assets/css/index.css";

import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

export default {
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(window.localStorage.getItem("todo")) || [
        { id: 100, name: "吃饭", isDone: true },
        { id: 201, name: "睡觉", isDone: false },
        { id: 103, name: "打豆豆", isDone: true },
      ],
      Btype: "all",
    };
  },
  methods: {
    addFn(name) {
      const obj = {
        id: Date.now(),
        name,
        isDone: false,
      };
      this.list.push(obj);
    },
    delTotoFn(index) {
      this.list.splice(index, 1);
    },
    changeType(type) {
      this.Btype = type;
    },
    clearDoFn() {
      this.list = this.list.filter((item) => item.isDone === false);
    },
  },
  computed: {
    showList() {
      if (this.Btype === "all") {
        return this.list;
      } else if (this.Btype === "no") {
        return this.list.filter((item) => item.isDone === false);
      } else {
        return this.list.filter((item) => item.isDone === true);
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler(newVal) {
        window.localStorage.setItem("todo", JSON.stringify(newVal));
      },
    },
  },
};
</script>